package internationalenergytradingplatform.controller.SupplierController;

import internationalenergytradingplatform.VO.CommodityDataVO;
import internationalenergytradingplatform.VO.OrderDataVO;
import internationalenergytradingplatform.VO.PackageDataVO;
import internationalenergytradingplatform.VO.TodayDataVO;
import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.SupplierAccountDao;
import internationalenergytradingplatform.pojo.SupplierAccount;
import internationalenergytradingplatform.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/home")
@CrossOrigin
@Slf4j
public class HomeController {

    @Autowired
    SupplierAccountDao supplierAccountDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     *
     * @author yingfeng
     * @date 2024/3/1 21:27
    传递公司信息
     */
    @GetMapping("/CompanyInformation")
    public Result<SupplierAccount> getCompanyInformation(){
        SupplierAccount companyByTele = supplierAccountDao.getCompanyByTele(BaseContext.getCurrentId());
        return Result.success(companyByTele);
    }
    /** 
     * 
     * @author yingfeng
     * @date 2024/3/1 23:04
     传递今日数据
     */
    @GetMapping("/todayData")
    public Result<TodayDataVO> getTodayData(){
        String companyId = BaseContext.getCurrentId();//获取公司id
        LocalDate now = LocalDate.now();//获取当前日期
        double turnover = supplierAccountDao.getTurnover(companyId, now.toString()) == null ? 0 : supplierAccountDao.getTurnover(companyId, now.toString());
        Integer newOrderQuantity = supplierAccountDao.getNewOrderQuantity(companyId, now.toString());
        Integer ordersInTransit = supplierAccountDao.getOrdersInTransit(companyId);
        Integer newPositiveReviews = supplierAccountDao.getNewPositiveReviews(companyId);
        Integer newlyAddedNegativeReviews = supplierAccountDao.getNewlyAddedNegativeReviews(companyId);
        TodayDataVO todayDataVO = new TodayDataVO();
        todayDataVO.setTurnover(turnover);
        todayDataVO.setNewOrderQuantity(newOrderQuantity);
        todayDataVO.setOrdersInTransit(ordersInTransit);
        todayDataVO.setNewPositiveReviews(newlyAddedNegativeReviews);
        todayDataVO.setNewlyAddedNegativeReviews(newPositiveReviews);
        return Result.success(todayDataVO);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/2 15:30
    传递订单管理
     */
    @GetMapping("/orderData")
    public Result<OrderDataVO> getOrderData(){
        String companyId = BaseContext.getCurrentId();//获取公司id
        //待运输商接单的订单
        Integer pendingOrders = supplierAccountDao.getOrderData(companyId, 1);
        //待发货的订单
        Integer toBeShipp = supplierAccountDao.getOrderData(companyId, 2);
        //待采购商确认的订单
        Integer toBeConfirm = supplierAccountDao.getOrderData(companyId, 3);
        //已完成的订单
        Integer complet = supplierAccountDao.getOrderData(companyId, 4);
        //全部订单
        Integer allOrders = supplierAccountDao.getAllCountOrder(companyId);
        OrderDataVO orderDataVO = new OrderDataVO();
        orderDataVO.setPendingOrders(pendingOrders);
        orderDataVO.setToBeShipp(toBeShipp);
        orderDataVO.setToBeConfirm(toBeConfirm);
        orderDataVO.setComplet(complet);
        orderDataVO.setAllOrders(allOrders);
        return Result.success(orderDataVO);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/3 13:12
     传递商品总览
     */
    @GetMapping("/commodityData")
    public Result<CommodityDataVO> getCommodityData(){
        String companyId = BaseContext.getCurrentId();//获取公司id
        //获取已上架的商品数量
        Integer numberProductsOnSale = supplierAccountDao.getCommodityData(companyId, 1);
        //获取已下架的商品数量
        Integer numberDiscontinuedProducts = supplierAccountDao.getCommodityData(companyId, 2);
        CommodityDataVO commodityDataVO = new CommodityDataVO();
        commodityDataVO.setNumberProductsOnSale(numberProductsOnSale);
        commodityDataVO.setNumberDiscontinuedProducts(numberDiscontinuedProducts);
        log.info("商品数据:{}", commodityDataVO);
        return Result.success(commodityDataVO);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/3 13:12
    传递套餐总览
     */
    @GetMapping("/packageData")
    public Result<PackageDataVO> getPackageData(){
        String companyId = BaseContext.getCurrentId();//获取公司id
        //获取已上架的商品数量
        Integer numberProductsOnSale = supplierAccountDao.getPackageData(companyId, 1);
        //获取已下架的商品数量
        Integer numberDiscontinuedProducts = supplierAccountDao.getPackageData(companyId, 2);
        PackageDataVO packageDataVO = new PackageDataVO();
        packageDataVO.setNumberProductsOnSale(numberProductsOnSale);
        packageDataVO.setNumberDiscontinuedProducts(numberDiscontinuedProducts);
        return Result.success(packageDataVO);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/29 0:08
    同步营业状态
     */
    @GetMapping("/synchronization")
    public Result synchronization(){
        String businessStatus = stringRedisTemplate.opsForValue().get("businessStatus");
        if(businessStatus.equals("1")) {
            return Result.success();
        } else {
            return Result.error("打烊咯~");
        }
    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/29 0:25
     更改营业状态
     */
    @PutMapping("/turnBusinessStatus")
    public Result turnBusinessStatus(){
        String businessStatus = stringRedisTemplate.opsForValue().get("businessStatus");
        ValueOperations<String, String> valueOps = stringRedisTemplate.opsForValue();
        if(businessStatus.equals("1")) {
            valueOps.set("businessStatus", "0"); // 设置为打烊状态
            return Result.success("已打样");
        } else {
            valueOps.set("businessStatus", "1"); // 设置为营业状态
            return Result.success("已营业");
        }

    }

}
